package manager;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import data.dao.*;
import data.dto.*;
import data.dao.daoFactory.*;
import data.dao.daoImplementation.NotFoundException;
import data.searchdto.HolidaySearchDTO;
import data.searchdto.LeaveSearchDTO;

public class LeaveManager {
	private LeaveDAO leaveDAO;
	private HolidaysDAO holidayDAO;

	private ArrayList<LeaveDTO> leavelist;
	private ArrayList<HolidaysDTO> holidaylist;

	public LeaveManager() {
		this.leaveDAO = LeaveFactory.loadInstance().getLeaveDAO();
		this.holidayDAO = LeaveFactory.loadInstance().getHolidaysDAO();
	}

	public ArrayList<LeaveDTO> findAllLeave() throws LeaveDAOException,
			SQLException {

		leavelist = new ArrayList<LeaveDTO>();
		try {
			leavelist = leaveDAO.findAllLeave();
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
		return leavelist;
	}

	public ArrayList<LeaveDTO> findLeave(LeaveSearchDTO leave)
			throws LeaveDAOException, SQLException {
		leavelist = new ArrayList<LeaveDTO>();
		try {
			leavelist = leaveDAO.findLeave(leave);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
		return leavelist;
	}

	public ArrayList<LeaveDTO> findLeavebyUser(int userid)
			throws LeaveDAOException, SQLException {
		leavelist = new ArrayList<LeaveDTO>();
		try {
			leavelist = leaveDAO.findLeavebyUser(userid);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
		return leavelist;
	}

	public ArrayList<LeaveDTO> findLeavebyUsers(short status,
			ArrayList<UserHierarchyDetailDTO> userids) {
		leavelist = new ArrayList<LeaveDTO>();
		try {
			leavelist = leaveDAO.findLeavebyUsers(status, userids);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return leavelist;
	}

	public void insertLeaveDetail(LeaveDTO LeaveDetail, HttpSession seesion)
			throws LeaveDAOException, SQLException {

		try {
			leaveDAO.insertLeaveDetail(LeaveDetail, seesion);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
	}

	public void updateLeaveDetail(LeaveDTO LeaveDetail)
			throws LeaveDAOException, SQLException, NotFoundException {
		try {
			leaveDAO.updateLeaveDetail(LeaveDetail);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}

	}

	public void updateLeaveDetail(short status, int id)
			throws LeaveDAOException, SQLException, NotFoundException {
		try {
			leaveDAO.updateLeaveDetail(status, id);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}

	}

	public void deleteLeaveDetail(LeaveDTO LeaveDetail)
			throws LeaveDAOException, SQLException, NotFoundException {
		try {
			leaveDAO.deleteLeaveDetail(LeaveDetail);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<HolidaysDTO> findHoiliday(HolidaySearchDTO leaveSearchdto)
			throws LeaveDAOException, SQLException {
		holidaylist = new ArrayList<HolidaysDTO>();
		try {
			holidaylist = holidayDAO.findHoiliday(leaveSearchdto);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
		return holidaylist;
	}

	public ArrayList<HolidaysDTO> findAllHoliday() throws LeaveDAOException,
			SQLException {
		holidaylist = new ArrayList<HolidaysDTO>();
		try {
			holidaylist = holidayDAO.findAllHoliday();
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
		return holidaylist;
	}

	public void insertHoliday(HolidaysDTO LeaveDetail)
			throws LeaveDAOException, SQLException {
		try {
			holidayDAO.insertHoliday(LeaveDetail);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
	}

	public void updateHoliday(HolidaysDTO LeaveDetail)
			throws LeaveDAOException, SQLException, NotFoundException {
		try {
			holidayDAO.updateHoliday(LeaveDetail);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
	}

	public void deleteHoliday(int LeaveDetail) throws LeaveDAOException,
			SQLException, NotFoundException {
		try {
			holidayDAO.deleteHoliday(LeaveDetail);
		} catch (LeaveDAOException e) {
			e.printStackTrace();
		}
	}

	public short findremainingleave(int createdby, int leavetype, int usertype) throws SQLException 
			 {
		float totalcount = 0;
		try {
			String sql = "select COUNT(*) as count from leavedb.leave"+
			" where leavetype = "+leavetype+" AND (status = 1 OR status = 2) AND createdby = "+createdby;
				
			float usedorpending = leaveDAO.findremainingleave(createdby, leavetype, usertype,sql );
			
			if(leavetype == 1){
				sql = "select annualleave as count from leavedb.usertype where id = "+usertype;
			}
			else if(leavetype == 2){
				sql = "select medicalleave as count from leavedb.usertype where id = "+usertype;
			}
			else if(leavetype == 3){
				sql = "select compensationleave as count from leavedb.usertype where id = "+usertype;
			}
			
			totalcount = leaveDAO.findremainingleave(createdby, leavetype, usertype,sql );
			totalcount = totalcount - usedorpending;
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (short)totalcount;
	}
}
